Skip to content

Type a method call by its receiver's class so Python and JavaScript truncate b.size() / 2 [patch] - #281

Merged
matt-edmondson merged 1 commit into
mainfrom
claude/coder-280-receiver-call-static-type
Oct 20, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
claude/coder-280-receiver-call-static-type

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #280

What was wrong

#273 (PR #276) gave StandardLanguageGenerator.StaticType a case for receiver-less calls only. A call with a receiver still came out as unknown type, so IsIntegerArithmetic returned false and the Python and JavaScript / and % overrides kept true division. For a = -7, b.doubled(a) / 4 gave -3.5, while C# and the C family give -3. That happened even with Box.doubled declared int and b typed Box.

Change

  • New CallExpression { Receiver: not null } case in StaticType, which calls DeclaredMethodReturnType(StaticType(receiver), callee).
  • DeclaredMethodReturnType resolves a type only when all of these hold:
    • the receiver's type is known and is not an array;
    • exactly one ClassDeclaration in GenerationRoot has that name;
    • exactly one method among that class's own members has the callee's name.
  • Anything else stays unknown, as Python and JavaScript use true division on a call result: twice(a) / 4 gives -3.5 where the other targets give -3 #273 does for overloads and undeclared callees. That covers an untyped receiver, an unknown or duplicated class, an overloaded method, and an inherited method. ExpectedType still takes precedence.

Tests

Added to Coder.Test/Languages/CallResultIntegerDivisionTests.cs:

  • Python_MethodCallResultOnATypedReceiver_MatchesTheCFamily and JavaScript_MethodCallResultOnATypedReceiver_MatchesTheCFamily run the generated code. They check / and %, both into a typed local and in a return, for arguments -7, 7, -9, 9 and 0.
  • UntypedReceiverUnknownClassOrOverloadedMethod_StaysTrueDivision checks that an overloaded method, an undeclared class and an untyped receiver all leave the division alone.

I checked the two run tests against the code without the fix and both fail as the issue describes (half_local(-7) = -3.5, expected -3). With the fix they pass. The full suite passes locally: 1324 succeeded, 0 failed, 0 skipped, with Python, Node, Go, Rust and gcc on the path.


Generated by Claude Code

…runcate b.size() / 2 [patch]

#273 typed only receiver-less calls, so a call on a receiver still fell
through to unknown type and the Python and JavaScript division and
remainder overrides kept true division and flooring: `b.doubled(a) / 4`
gave -3.5 for a = -7 where the C family gives -3, even with `doubled`
declared `int` on `Box` and `b` typed `Box`.

A call with a receiver now takes the receiver's static type, finds the one
ClassDeclaration in the tree with that name, and uses the return type of
the one method among its own members with the callee's name. An untyped
receiver, a class declared nowhere or more than once, an inherited method
and an overload all stay unknown, which keeps today's behaviour. An
ExpectedType still wins.

Fixes #280

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tgs9bqa4b49KCmByF6PHfP
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python and JavaScript still use true division on a method call with a receiver: b.size() / 2 gives -3.5 where C# gives -3

2 participants